package com.urbo.android.shortcutter;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

public abstract class UrboDialogActivity extends Activity {

	private int mLayoutRootID;
	private static int BUTTON_OK = 990001;
	private static int BUTTON_CANCEL = 990002;
	public static String RESULT_BUNDLE = "result_bundle";
	private Intent result;

	// Derived class must implement its own method to set resulting data
	protected abstract void setReturnData();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onStart()
	{
		super.onStart();
	}

	protected void setContentView(int layoutResID, int layoutRootID) {
		mLayoutRootID = layoutRootID;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.setContentView(layoutResID);
	}
	
	Button.OnClickListener onClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View v) {
			if (v.getId() == BUTTON_OK) {
				result = new Intent();
				setReturnData();
				setResult(RESULT_OK, result);
				finish();
			} else if (v.getId() == BUTTON_CANCEL){
				setResult(RESULT_CANCELED, null);
				finish();
			}
		}
	};
	
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);

		LinearLayout buttonsLayout = new LinearLayout(this);
	
		LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT, 0.5f);	
		buttonLayoutParams.gravity = Gravity.CLIP_VERTICAL;
		
		buttonsLayout.setOrientation(LinearLayout.HORIZONTAL);
		buttonsLayout.setWeightSum(1);
		buttonsLayout.setLayoutParams(buttonLayoutParams);
		buttonsLayout.setBackgroundColor(Color.GRAY);
		buttonsLayout.setPadding(0, 3, 0, 0);
		
		Button btn = new Button(this);
		btn.setId(BUTTON_OK);
		btn.setText("OK");
		btn.setLayoutParams(buttonLayoutParams);
		btn.setOnClickListener(onClickListener);
		buttonsLayout.addView(btn);
		
		btn = new Button(this);
		btn.setId(BUTTON_CANCEL);
		btn.setText("Cancel");
		btn.setOnClickListener(onClickListener);
		btn.setLayoutParams(buttonLayoutParams);
		buttonsLayout.addView(btn);

		LinearLayout rootLayout = (LinearLayout)this.findViewById(mLayoutRootID);
		rootLayout.addView(buttonsLayout);
	}
	
	
	protected void putExtra(String id, Bundle bundle) {
		result.putExtra(id, bundle);
	}

}
